package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.CategoryMapper;
import com.goods.business.service.CategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.models.auth.In;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Stream;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 获取物资类别Tree
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVO<ProductCategoryTreeNodeVO> CategoryTree(Integer pageNum, Integer pageSize) {
        List<ProductCategoryTreeNodeVO> productCategoryList = categoryMapper.selectProductCategory();
        List list1 = CategoryTreeBuilder.build(productCategoryList);
        return new PageVO<>(new PageInfo(list1).getTotal(),ListPageUtils.page(list1,pageSize,pageNum));
    }

    /**
     * 获取所有父分类
     * @return
     */
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryList = categoryMapper.selectProductCategory();
        List list = CategoryTreeBuilder.buildParent(productCategoryList);
        return list;
    }

    /**
     * 添加分类
     * @param productCategoryVO
     * @return
     */
    @Override
    public void addProductCategory(ProductCategoryVO productCategoryVO) {
        categoryMapper.addCategory(productCategoryVO);
    }

    /**
     * 根据id回显分类信息
     * @param id
     * @return
     */
    @Override
    public ProductCategoryVO edit(Long id) {
        ProductCategoryVO productCategoryVO = categoryMapper.selectProductCategoryById(id);
        return productCategoryVO;
    }

    /**
     * 修改分类
     * @param productCategoryVO
     * @return
     */
    @Override
    public void updateProductCategory(ProductCategoryVO productCategoryVO,Long id) {
        categoryMapper.updateProductCategory(productCategoryVO);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public void deleteById(Long id) {
        categoryMapper.deleteById(id);
    }
}
